//
//  THSubjectTaskVC.m
//  YJB
//
//  Created by 中电大维 on 2016/12/19.
//  Copyright © 2016年 YJB. All rights reserved.
///Users/zuoling/Desktop/YJB/YJB/Home/TuHao/Home/CPC/VC/THSubjectTaskVC.m:14:9: In file included from /Users/zuoling/Desktop/YJB/YJB/Home/TuHao/Home/CPC/VC/THSubjectTaskVC.m:14:

#import "THSubjectTaskVC.h"
#import "masonry.h"
#import "YJUserDefaultTool.h"
#import "YJNetworkTool.h"
#import "UIImageView+WebCache.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import "TZImagePickerController.h"
#import "TZImageManager.h"

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

@interface THSubjectTaskVC ()<UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource,TZImagePickerControllerDelegate>

@property(nonatomic, strong)UIView *headView;
@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIButton *submitButton;
@property(nonatomic, strong)UIButton *cancelButton;

@property(nonatomic, strong)NSMutableArray *submitImages;
@property(nonatomic, strong)NSMutableArray *descriptionImages;
@property(nonatomic, strong)NSMutableArray *submitUrls;
@property(nonatomic, strong)NSMutableArray *returnImages;

@property(nonatomic,strong)NSMutableArray *selectedPhotos;
@property(nonatomic,strong)NSMutableArray *selectedAssets;

@end

@implementation THSubjectTaskVC
{
    UILabel *_titleLabel;
    UILabel *_needLabel;
    UILabel *_requireLabel;
    UILabel *_imageLabel;
    UIView  *_rectView;
    UIImageView *_backImage;
    
    UITextView *_resultTextView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _selectedPhotos = [NSMutableArray array];
    _selectedAssets = [NSMutableArray array];
    
    self.title = @"做任务";
    [self addView];
    [self loadData];
}

- (void)uploadImg
{
    NSString *uploadUrl = THURL(@"upload_img.php");
    [YJNetworkTool uploadWithUrl:uploadUrl parameters:nil bodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i = 0; i < self.submitImages.count; i ++)
        {
            UIImage *selectImage = self.submitImages[i];
            NSData *selectImageData = UIImageJPEGRepresentation(selectImage, 1.0);
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            NSString *fileName = [NSString stringWithFormat:@"%@%d.png",[formatter stringFromDate:[NSDate date]],i];
            
            [formData appendPartWithFileData:selectImageData name:@"file[]" fileName:fileName mimeType:@"image/png"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nonnull responseObject) {
        MyLog(@"做任务上传图返回：%@",responseObject);
        self.submitUrls = responseObject[@"return"][@"data"][@"img_list"];
        [self submitAllInfomation];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        MyLog(@"上传失败 ======== %@",error);
    }];
}

- (void)loadData
{
    [MBProgressHUD showMessage:@"正在获取详情" toView:nil];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5Key = [YJUserDefaultTool readKeyID];
    
    params[@"action"] = @"get_donetask_one";
    params[@"makegold"] = @{@"pubid":self.pubid,
                            @"firstrow":@"-1",
                            @"listrows":@"",
                            @"user_info":userName,
                            @"phone_info":@"iOS",
                            @"md5_key":md5Key};
    [YJNetworkTool postWithUrl:THURL(@"task.php") parameters:params success:^(id responseObject) {
        NSString *code = responseObject[@"return"][@"code"];

        [MBProgressHUD hideHUDForView:nil];
        NSLog(@"%@",responseObject);
        
        if (code.integerValue == 200)
           {
            NSDictionary *dict = [[responseObject objectForKey:@"return"] objectForKey:@"data"];
            
            NSString *desImageStr = [[[responseObject objectForKey:@"return"] objectForKey:@"data"] objectForKey:@"image"];
            self.descriptionImages = [self analysisImageUrl:desImageStr];
            //[self getReturnImage];
            [self createView];
            [self setData:dict];
        }
        else if(code.integerValue == 500)
           // [_tasks removeAllObjects];
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:nil];
    }];
}

- (NSMutableArray *)analysisImageUrl:(NSString *)imageUrl
{
    NSMutableArray *imageUrls = [[NSMutableArray alloc] init];
    if (imageUrl.length>2) {
        imageUrl = [imageUrl substringWithRange:NSMakeRange(1, imageUrl.length-2)];
        NSArray *objects = [imageUrl componentsSeparatedByString: @","];
        for (int i = 0; i < objects.count; i++) {
            NSString *subUrl = objects[i];
            subUrl = [subUrl substringWithRange:NSMakeRange(1, subUrl.length-2)];
            NSArray *array = [subUrl  componentsSeparatedByString:@":"];
            if (array.count == 2) {
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                NSString *valueStr = array[1];
                NSString *keyStr = array[0];
                [dict setObject:[valueStr substringWithRange:NSMakeRange(1, valueStr.length-2)] forKey:[keyStr substringWithRange:NSMakeRange(1, keyStr.length-2)]];
                [imageUrls addObject:dict];
            }
        }
    }
    return imageUrls;
}


- (void)submit:(UIButton *)button
{
    [MBProgressHUD showMessage:@"正在提交" toView:self.view];
    if (self.submitImages.count) {
        [self uploadImg];
    }else{
        [self submitAllInfomation];
    }
}

- (void)submitAllInfomation
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5Key = [YJUserDefaultTool readKeyID];
    
    params[@"action"] = @"set_task_done";
    
    if (self.submitUrls.count) {
        params[@"makegold"] = @{@"pubid":self.pubid,
                                @"username":userName,
                                @"content":_resultTextView.text,
                                @"user_info":userName,
                                @"phone_info":@"iOS",
                                @"md5_key":md5Key,
                                @"image":self.submitUrls};

    }else{
        params[@"makegold"] = @{@"pubid":self.pubid,
                                @"username":userName,
                                @"content":_resultTextView.text,
                                @"user_info":userName,
                                @"phone_info":@"iOS",
                                @"md5_key":md5Key};

    }
    
       [YJNetworkTool postWithUrl:THURL(@"task.php") parameters:params success:^(id responseObject) {
        NSString *code = responseObject[@"return"][@"code"];
        [MBProgressHUD hideHUDForView:self.view];
        if (code.integerValue != 200)
        {
            [MBProgressHUD showError:responseObject[@"return"][@"message"] toView:self.view];
           // [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showSuccess:@"提交成功"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];

}
- (void)setData:(NSDictionary *)dict{

    NSString *imgStr = self.descriptionImages.count>0?[NSString stringWithFormat:@"%lu张",(unsigned long)self.descriptionImages.count]:@"无图";
    _titleLabel.text = [dict objectForKey:@"title"];
    _needLabel.text = [NSString stringWithFormat:@"需要：%@",[dict objectForKey:@"need"]];
    _requireLabel.text = [NSString stringWithFormat:@"要求：%@",[dict objectForKey:@"class"]];;
    _imageLabel.text = [NSString stringWithFormat:@"任务截图：%@",imgStr];
    _resultTextView.text = [dict objectForKey:@"content"];
    
}

- (void)cancel:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addImage:(UIButton *)button
{
    if (button.tag-2 == self.submitImages.count) {
       [self presentImagePickVC];
    }
}



#pragma mark ----------------------- 图片选择器的代理方法
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos
{
    for (UIImage *image in photos) {
        //    将原图尺寸进行缩小
        [image imageByScaledToSize:CGSizeMake(300, 300)];
        [self.submitImages addObject:image];
        
        for (int i = 0; i<=self.submitImages.count; i++) {
            UIButton *button = [self.scrollView viewWithTag:i+2];
            button.hidden = NO;
            if (i < self.submitImages.count) {
                [button setBackgroundImage:self.submitImages[i] forState:UIControlStateNormal];
            }
        }
    }
    self.selectedPhotos = [photos mutableCopy];
    self.selectedAssets = [assets mutableCopy];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"])
    {
        TZImagePickerController *tzImagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
        tzImagePickerVc.sortAscendingByModificationDate = YES;
        [tzImagePickerVc showProgressHUD];
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        // save photo and get asset / 保存图片，获取到asset
        [[TZImageManager manager] savePhotoWithImage:image completion:^{
            [[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model) {
                [[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models) {
                    [tzImagePickerVc hideProgressHUD];
                    TZAssetModel *assetModel = [models firstObject];
                    if (tzImagePickerVc.sortAscendingByModificationDate) {
                        assetModel = [models lastObject];
                    }
                    [self.selectedAssets addObject:assetModel.asset];
                    [self.selectedPhotos addObject:image];
                    
//                    [image imageByScaledToSize:CGSizeMake(300, 300)];
                    [self.submitImages addObject:image];
                    
                    for (int i = 0; i<=self.submitImages.count; i++) {
                        UIButton *button = [self.scrollView viewWithTag:i+2];
                        button.hidden = NO;
                        if (i < self.submitImages.count) {
                            [button setBackgroundImage:self.submitImages[i] forState:UIControlStateNormal];
                        }
                    }
//                    [self.tableView reloadData];
                }];
            }];
        }];
    }
}

- (void)presentImagePickVC
{
    TZImagePickerController *imagePickerVC = [[TZImagePickerController alloc]initWithMaxImagesCount:9 delegate:self];
    imagePickerVC.selectedAssets = self.selectedAssets;
    //导航栏颜色
    imagePickerVC.navigationBar.tintColor = WHITECOLOR;
    imagePickerVC.navigationBgImage = @"nav_backGround";
    //不要拍照按钮
    imagePickerVC.allowTakePicture = NO;
    //不能选择视频
    imagePickerVC.allowPickingVideo = NO;
    //不允许选择原图
    imagePickerVC.allowPickingOriginalPhoto = NO;
    imagePickerVC.maxImagesCount = 4;
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

//- (void)presentImagePickVC
//{
//    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
//    imagePickerController.delegate = self;
//    imagePickerController.allowsEditing = YES;
//    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    [self presentViewController:imagePickerController animated:YES completion:nil];
    //弹出图片选择器
//}

- (void)getReturnImage
{
    // 创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("cn.xxx.queue", DISPATCH_QUEUE_SERIAL);

    [self.descriptionImages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        NSString *str = [NSString stringWithFormat:@"http://%@",[self.descriptionImages[idx] objectForKey:@"img_url"]];
        // 开启异步函数，获取下载图片，获取尺寸
        dispatch_async(queue, ^{
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
            UIImage *image = [UIImage imageWithData:data];
            [self.returnImages addObject:image];
            // 回到主线程执行
            dispatch_sync(dispatch_get_main_queue(), ^(){
                [self.tableView reloadData];
            });
        });
    }];
}


#pragma mark - UITableViewDelegate,UITableData
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.descriptionImages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubjectTask"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SubjectTask"];
        UIImageView *image = [[UIImageView alloc] init];
        image.image = Image(@"task_placeholderImg_icon");
        image.tag = 1;
        [cell.contentView addSubview:image];
    }

//    UIImageView *imageView = [cell viewWithTag:1];
//    if (self.returnImages.count > indexPath.row) {
//        UIImage *image = self.returnImages[indexPath.row];
//        imageView.image = image;
//        
//        imageView.height = tableView.width/(image.size.width/image.size.height);
//        CGRect rect = cell.frame;
//        rect.size.height = imageView.height+2.5;
//        cell.frame = rect;
//    }
    
    UIImageView *imageView = [cell viewWithTag:1];
    imageView.frame = CGRECT(0,0,tableView.width,tableView.rowHeight);
    NSString *str = [NSString stringWithFormat:@"http://%@",[self.descriptionImages[indexPath.row] objectForKey:@"img_url"]];
    [imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:Image(@"task_placeholderImg_icon") options:(SDWebImageRetryFailed) completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        imageView.height = tableView.width/(image.size.width/image.size.height);
        CGRect rect = cell.frame;
        rect.size.height = imageView.height+2.5;
        cell.frame = rect;
    }];
    
    return cell;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
//    NSLog(@"cell height %f",cell.frame.size.width);
//    return cell.frame.size.width;
//}



//#pragma mark - 图片代理
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//    //    将原图尺寸进行缩小
//    image = [image imageByScaledToSize:CGSizeMake(300, 300)];
//    [self.submitImages addObject:image];
//    
//    for (int i = 0; i<=self.submitImages.count; i++) {
//        UIButton *button = [self.scrollView viewWithTag:i+2];
//        button.hidden = NO;
//        if (i < self.submitImages.count) {
//            [button setBackgroundImage:self.submitImages[i] forState:UIControlStateNormal];
//        }
//    }
//    [self dismissViewControllerAnimated:YES completion:^{}];
//    
//}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView{
    [textView resignFirstResponder];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_resultTextView resignFirstResponder];
}

- (void)addView{
    [self.view addSubview:self.self.headView];
}

- (void)createView{
    CGRect backImageRect = CGRECT(12, self.headView.y+self.headView.height+12, self.view.width-24, GRIDW*36);
    _backImage = [[UIImageView alloc] initWithFrame:backImageRect];
    _backImage.image = Image(@"publis_input_bg");
    _backImage.userInteractionEnabled = YES;
    [self.view addSubview:_backImage];
    [_backImage addSubview:self.scrollView];
    
//    [_backImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.headView.mas_bottom).offset(12);
//        make.left.equalTo(self.view.mas_left).offset(12);
//        make.right.equalTo(self.view.mas_right).offset(-12);
//        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.68);
//    }];
    
    NSDictionary *attrs = @{NSFontAttributeName : FONT(15)};
    NSString *string = @"任务标题";
    CGSize textSize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT,40) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    CGRect titleRect = CGRECT(7.5, 15, textSize.width, 18);
    UILabel *titleLabel = [UILabel initLabel:titleRect Text:string Font:15 TextColor:kUIColorFromRGB(0x666666)];
    [self.scrollView addSubview:titleLabel];
    
    CGRect line1Rect = CGRECT(titleLabel.rightX+15, titleLabel.bottomY, _backImage.width-titleLabel.rightX-30, 0.5);
    UIView *line1 = [[UIView alloc] initWithFrame:line1Rect];
    line1.backgroundColor = kUIColorFromRGB(0x999999);
    [self.scrollView addSubview:line1];
    
    CGRect _titleRect = CGRECT(line1.x, line1.y-titleLabel.height, line1.width, titleLabel.height);
    _titleLabel = [UILabel initLabel:_titleRect Text:@"流量任务测试发布" Font:15 TextColor:NAVTITLECOLOR];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:_titleLabel];
    
    CGRect descriptionRect = CGRECT(titleLabel.x, titleLabel.bottomY+15, titleLabel.width, 18);
    UILabel *descriptionLabel = [UILabel initLabel:descriptionRect Text:@"任务描述" Font:15 TextColor:kUIColorFromRGB(0x666666)];
    [self.scrollView addSubview:descriptionLabel];
    
    CGRect descView_Rect = CGRECT(descriptionLabel.x, descriptionLabel.bottomY+7.5, self.scrollView.width-descriptionLabel.x*2, 7.5*4+16*3);
    _rectView = [[UIView alloc] initWithFrame:descView_Rect];
    [_rectView.layer setBorderWidth:0.5];
    [_rectView.layer setBorderColor:kUIColorFromRGB(0x999999).CGColor];
    [self.scrollView addSubview:_rectView];
    
    CGRect needRect = CGRECT(_rectView.x+7.5, _rectView.y+7.5, _backImage.width-15, 16);
    _needLabel = [UILabel initLabel:needRect Text:@"需要：截图" Font:15 TextColor:kUIColorFromRGB(0x333333)];
    
    [self.scrollView addSubview:_needLabel];
    
    CGRect requireRect = CGRECT(_needLabel.x, _needLabel.bottomY+7.5, _needLabel.width, _needLabel.height);
    _requireLabel = [UILabel initLabel:requireRect Text:@"要求：转发文档" Font:15 TextColor:kUIColorFromRGB(0x333333)];
    [self.scrollView addSubview:_requireLabel];
    
    CGRect imagRect = CGRECT(_needLabel.x, _requireLabel.bottomY+7.5, _needLabel.width, _needLabel.height);
    _imageLabel = [UILabel initLabel:imagRect Text:@"截图：无图" Font:15 TextColor:kUIColorFromRGB(0x333333)];
    
    //(origin = (x = 15, y = 128), size = (width = 336, height = 16))

    [self.scrollView addSubview:_imageLabel];
    [self.scrollView addSubview:self.tableView];
    self.tableView.frame = CGRECT(0, _imageLabel.bottomY, self.scrollView.width*0.65, self.scrollView.width*0.65*self.descriptionImages.count);
    self.tableView.rowHeight = self.scrollView.width*0.65;
    self.tableView.centerX = self.scrollView.centerX;
    _rectView.height += self.tableView.height;
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.width, self.scrollView.height+self.tableView.height+50);
    
    CGRect resultRect = CGRECT(_rectView.x, _rectView.bottomY+15, _rectView.width, 16);
    UILabel *resultLabel = [UILabel initLabel:resultRect Text:@"提交结果" Font:15 TextColor:kUIColorFromRGB(0x666666)];
    [self.scrollView addSubview:resultLabel];
    
    CGRect line2Rect = CGRECT(resultLabel.x, resultLabel.bottomY+2.5, _rectView.width, 0.5);
    UIView *line2 = [[UIView alloc] initWithFrame:line2Rect];
    line2.backgroundColor = kUIColorFromRGB(0x999999);
    [self.scrollView addSubview:line2];
    
    _resultTextView = [[UITextView alloc] initWithFrame:CGRECT(line2.x, line2.bottomY+7.5, line2.width, 50)];
    _resultTextView.text = @"已完成任务，请审核";
    _resultTextView.textColor = NAVTITLECOLOR;
    _resultTextView.delegate = self;
    [self.scrollView addSubview:_resultTextView];

    float width = (self.scrollView.width-15-10*3)/4.0;
    for (int i = 0; i < 4; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRECT(7.5+(i*(10+width)), _resultTextView.bottomY+7.5, width, width)];
        [button setBackgroundImage:Image(@"task_placeholderImg_icon") forState:UIControlStateNormal];
        [self.scrollView addSubview:button];
        button.hidden = i;
        button.tag = i+2;
        
        [button addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIView *line3 = [[UIView alloc] initWithFrame:CGRECT(line2.x,_resultTextView.bottomY+width+7.5+10, line2.width, line2.height)];
    line3.backgroundColor = kUIColorFromRGB(0x999999);
    line3.tag = 1;
    [self.scrollView addSubview:line3];
    
    [self.scrollView addSubview:self.submitButton];
    [self.scrollView addSubview:self.cancelButton];
}

- (UIButton *)submitButton
{
    UIView *line = [self.scrollView viewWithTag:1];
    CGFloat x = (self.scrollView.width-85*2-60)/2.0;
    CGRect rect = CGRECT(x, line.bottomY+25, 85, 44);
    if (!_submitButton) {
        _submitButton = [[UIButton alloc] initWithFrame:rect];
        [_submitButton setTitle:@"提交" forState:UIControlStateNormal];;
        
        [_submitButton setBackgroundImage:Image(@"publish_Btn") forState:UIControlStateNormal];
        [_submitButton setBackgroundImage:Image(@"publish_Btn_cllick") forState:UIControlStateHighlighted];
        [_submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitButton;
}

- (UIButton *)cancelButton
{
    UIView *line = [self.scrollView viewWithTag:1];
    CGRect rect = CGRECT(self.submitButton.rightX+60,line.bottomY+25, 85, 44);
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] initWithFrame:rect];
        //_cancelButton.backgroundColor = [UIColor orangeColor];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setBackgroundImage:Image(@"publish_Btn") forState:UIControlStateNormal];
        [_cancelButton setBackgroundImage:Image(@"publish_Btn_cllick") forState:UIControlStateHighlighted];
        [_cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIView *)headView
{
    if (_headView == nil)
    {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = WHITECOLOR;
        [self.view addSubview:_headView];
        [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.view.mas_top);
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.height.mas_equalTo(60);
        }];
        UILabel *tip = [[UILabel alloc]init];
        tip.text = @"官方提示:";
        tip.textAlignment = NSTextAlignmentCenter;
        tip.font = FONT(14);
        tip.textColor = kUIColorFromRGB(0xd01e06);
        [_headView addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(12);
            make.height.mas_equalTo(18);
            make.width.mas_equalTo(86);
        }];
        
        UILabel *tipTwo = [[UILabel alloc]init];
        tipTwo.text = @"        如要求出钱或发短信才能完成的任务,请不要做,谢谢!";
        tipTwo.font = FONT(12);
        tipTwo.textColor = kUIColorFromRGB(0x222222);
        [_headView addSubview:tipTwo];
        [tipTwo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_headView).offset(0);
            make.top.equalTo(tip.mas_bottom).offset(5);
        }];
    }
    return _headView;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        
        CGRect rect = CGRECT(1, 1, _backImage.width-2, _backImage.height-2);
        UIScrollView  *scrollView = [[UIScrollView alloc] initWithFrame:rect];
        scrollView.userInteractionEnabled = YES;
        scrollView.delegate = self;
        // 是否反弹
        scrollView.bounces = NO;
        [scrollView flashScrollIndicators];
        scrollView.directionalLockEnabled = YES;
        _scrollView = scrollView;
    }
    return _scrollView;
}

- (NSMutableArray *)submitImages
{
    if (!_submitImages) {
        _submitImages = [[NSMutableArray alloc] init];
    }
    return _submitImages;
}

- (NSMutableArray *)descriptionImages
{
    if (!_descriptionImages) {
        _descriptionImages = [[NSMutableArray alloc] init];
    }
    return _descriptionImages;
}

- (NSMutableArray *)submitUrls
{
    if (!_submitUrls) {
        _submitUrls = [[NSMutableArray alloc] init];
    }
    return _submitUrls;
}
- (NSMutableArray *)returnImages
{
    if (!_returnImages) {
        _returnImages = [[NSMutableArray alloc] init];
    }
    return _returnImages;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.rowHeight = _tableView.width;
        //_tableView.backgroundColor = [UIColor orangeColor];
    }
    return _tableView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
